#include "XmlSaxParser.h"
#include <string.h>

XmlSaxAttr::XmlSaxAttr(const XML_Char **attr)
	:m_count(0), m_atts(attr)
{

	for (int i = 0; attr[i]; i += 2) {
		//printf(" %s='%s'", attr[i], attr[i + 1]);
		++m_count;
	}
}

int XmlSaxAttr::GetCount()
{
	return m_count;
}

const XML_Char * XmlSaxAttr::GetNameAt(int i)
{
	if (i < m_count && i >= 0)
	{
		return m_atts[i * 2];
	}
	else
		return NULL;
}

const XML_Char * XmlSaxAttr::GetValueAt(int i)
{
	if (i < m_count && i >= 0)
	{
		return m_atts[i * 2 + 1];
	}
	else
		return NULL;
}

const XML_Char * XmlSaxAttr::Find(const XML_Char* name)
{
	for (int i = 0; m_atts[i]; i += 2) {
		//printf(" %s='%s'", attr[i], attr[i + 1]);
		if (strcmp(m_atts[i], name) == 0)
		{
			return m_atts[i + 1];
		}
	}
	return NULL;
}
/////////////////////////////////////////////////////////////////////////////////

void _XML_StartElementHandler(void *userData,
	const XML_Char *name,
	const XML_Char **atts)
{
	XmlSaxHandler* handler = (XmlSaxHandler*)userData;
	XmlSaxAttr attr(atts);
	handler->StartElementHandler(name, &attr);
}

void _XML_EndElementHandler(void *userData,
	const XML_Char *name)
{
	XmlSaxHandler* handler = (XmlSaxHandler*)userData;
	handler->EndElementHandler(name);
}

void _XML_CharacterDataHandler(void *userData,
	const XML_Char *s,
	int len)
{
	XmlSaxHandler* handler = (XmlSaxHandler*)userData;
	handler->CharacterDataHandler(s,len);
}

/* target and data are 0 terminated */
void _XML_ProcessingInstructionHandler(
	void *userData,
	const XML_Char *target,
	const XML_Char *data)
{
	XmlSaxHandler* handler = (XmlSaxHandler*)userData;
}


void _XML_CommentHandler(void *userData,
	const XML_Char *data)
{
	XmlSaxHandler* handler = (XmlSaxHandler*)userData;
	handler->CommentHandler(data);
}

void _XML_StartCdataSectionHandler(void *userData)
{
	XmlSaxHandler* handler = (XmlSaxHandler*)userData;
	handler->StartCdataSectionHandler();
}

void _XML_EndCdataSectionHandler(void *userData)
{
	XmlSaxHandler* handler = (XmlSaxHandler*)userData;
	handler->EndCdataSectionHandler();
}

void _XML_ElementDeclHandler(void *userData,
	const XML_Char *name,
	XML_Content *model)
{
	XmlSaxHandler* handler = (XmlSaxHandler*)userData;
	handler->ElementDeclHandler(name,model);
}

void _XML_AttlistDeclHandler(
	void            *userData,
	const XML_Char  *elname,
	const XML_Char  *attname,
	const XML_Char  *att_type,
	const XML_Char  *dflt,
	int              isrequired)
{
	XmlSaxHandler* handler = (XmlSaxHandler*)userData;
	handler->AttlistDeclHandler(elname, attname, att_type, dflt, isrequired);
}


XmlSaxParser::XmlSaxParser(const XML_Char *encoding,XmlSaxHandler* handler)
	:m_parser(XML_ParserCreate(encoding)),m_handler(handler)
{
	XML_SetUserData(m_parser, handler);
	XML_SetElementHandler(m_parser, _XML_StartElementHandler, _XML_EndElementHandler);
	XML_SetCharacterDataHandler(m_parser, _XML_CharacterDataHandler);
	XML_SetProcessingInstructionHandler(m_parser, _XML_ProcessingInstructionHandler);
	XML_SetCommentHandler(m_parser, _XML_CommentHandler);
	XML_SetCdataSectionHandler(m_parser, _XML_StartCdataSectionHandler, _XML_EndCdataSectionHandler);
	XML_SetElementDeclHandler(m_parser, _XML_ElementDeclHandler);
	XML_SetAttlistDeclHandler(m_parser, _XML_AttlistDeclHandler);
}


XmlSaxParser::~XmlSaxParser()
{
	XML_ParserFree(m_parser);
}


bool XmlSaxParser::Reset(const XML_Char *encoding)
{
	return XML_ParserReset(m_parser, encoding);
}

bool XmlSaxParser::SetEncoding(const XML_Char *encoding)
{
	return XML_SetEncoding(m_parser, encoding)== XML_STATUS_OK;
}

bool XmlSaxParser::Parse(const char *s, int len, int isFinal)
{
	return XML_Parse(m_parser, s, len, isFinal) == XML_STATUS_OK;
}


bool XmlSaxParser::StopParser(XML_Bool resumable)
{
	return XML_StopParser(m_parser,resumable)== XML_STATUS_OK;
}

bool XmlSaxParser::ResumeParser()
{
	return XML_ResumeParser(m_parser) == XML_STATUS_OK;
}









